package android.gpsmessenger;

import java.util.ArrayList;

import android.os.Bundle;
import android.content.Intent;
import android.app.ListFragment;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;

import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class ContactListFragment extends ListFragment
{
	ContactManager contactManager ;
	ContactList contacts;

	ContactAdapter contactAdapter;
	Contact selectedContact;
	ListView listView;
	String id;
	int layoutID ;
	
	public void onCreate (Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
		contactManager = new ContactManager(getActivity());

		contacts = new ContactList();
		contacts = contactManager.load();
	}
		
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		inflater.inflate(R.menu.fragment_list_menu, menu);
	}
	
	public void onResume() 
	{
		super.onResume();
		contacts = contactManager.load();
	    
	    listView.setAdapter(contactAdapter);		
	}

	public void onPause() 
	{
	    super.onPause();
	    contactManager.save(contacts);
	}	
		
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		getActivity().setContentView(layoutID);

		contacts = contactManager.load();
		listView  = (ListView)  getActivity().findViewById(R.id.listView1);
		
		contactAdapter = new ContactAdapter(getActivity(), R.layout.custom_row_view, new ArrayList<Contact>(100));
	    
	    listView.setAdapter(contactAdapter);
	    	    
	    listView.setClickable(true);
	    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	  
	    
	    listView.setOnItemClickListener(new OnItemClickListener() 
	    {
	        @Override
	        public void onItemClick(AdapterView<?> a, View v, int position, long id) 
	        {
	        	contactAdapter.setSelectedPosition(position);
	    		getActivity().invalidateOptionsMenu();
	        } 
	    });
    }
	
	public boolean onOptionsItemSelected (MenuItem item) 
	{
        Intent intent = new Intent(getActivity(), ContactProfileActivity.class);
        if (item.getItemId() == R.id.menu_new) 
		{
	        intent.putExtra("id", id);			
	        getActivity().startActivity(intent);	
		}
		if (item.getItemId() == R.id.menu_edit) 
		{
        	Object o = listView.getItemAtPosition(contactAdapter.getSelectedPosition());
        	selectedContact = (Contact) o;
        	
        	edit(intent, selectedContact);
		}
		if (item.getItemId() == R.id.menu_delete) 
		{
	       	Object o = listView.getItemAtPosition(contactAdapter.getSelectedPosition());
        	selectedContact = (Contact) o;
        	
			remove(selectedContact);
		}

		if (item.getItemId() == R.id.menu_home) 
		{
			getActivity().startActivity(new Intent(getActivity(), MainActivity.class));	
		}		
		return true;
	}	
	
	public void onPrepareOptionsMenu(Menu menu) 
	{
		super.onPrepareOptionsMenu(menu);
	    MenuItem delete = menu.findItem(R.id.menu_delete);
	    MenuItem edit = menu.findItem(R.id.menu_edit);
	    MenuItem select = menu.findItem(R.id.menu_select);
		if (contactAdapter.getSelectedPosition() != -1) 
		{
			delete.setEnabled(true);
			edit.setEnabled(true);
		}
		else 
		{
			delete.setEnabled(false);
			edit.setEnabled(false);			
		}
		select.setEnabled(false);
	}
	
	public void edit(Intent intent, Contact selectedContact)
	{
		intent.putExtra("name", selectedContact.getName());
		intent.putExtra("phone", selectedContact.getPhone());
    	intent.putExtra("email", selectedContact.getEmail());
    	intent.putExtra("id", id);
        
		getActivity().startActivity(intent);
        contactAdapter.setSelectedPosition(-1);
		getActivity().invalidateOptionsMenu();
	}
	
	public void remove(Contact contact){}

}